# 游戏节奏
from fish import *
from bullet import *
from utils import redis_utils

# 分组控制，部分内容(如子弹、鱼)都需要进行分组管理，同时需要判断碰撞
class GroupControl:
    def __init__(self) -> None:
        # 全部的鱼
        self.all_fishes = pygame.sprite.Group()
        # 子弹组
        self.bullets = pygame.sprite.Group()
        # 渔网组
        self.fish_nets = pygame.sprite.Group()

    """ 以下add系列方法都是将某中类型成员添加n个到组中 """

    def add_gold_fish(self, num: int):
        for i in range(num):
            gold_fish = GoldFish()
            gold_fish.reset()
            self.all_fishes.add(gold_fish)

    def add_octopus_fish(self, num: int):
        for i in range(num):
            clown_fish = OctopusFish()
            clown_fish.reset()
            self.all_fishes.add(clown_fish)

    def add_manta_fish(self, num: int):
        for i in range(num):
            manta_fish = MantaFish()
            manta_fish.reset()
            self.all_fishes.add(manta_fish)

    def add_shark_fish(self, num: int):
        for i in range(num):
            shark_fish = SharkFish()
            shark_fish.reset()
            self.all_fishes.add(shark_fish)

    def add_boss_fish(self, num: int):
        for i in range(num):
            boss_fish = BossFish()
            boss_fish.reset()
            self.all_fishes.add(boss_fish)

    def add_bullet(self, bullet: Bullet):
        self.bullets.add(bullet)

    def add_fish_net(self, fish_net):
        if len(self.fish_nets) >= 5:
            return
        self.fish_nets.add(fish_net)

    def add_fish_to_dict(self, fish_dict: dict):
        """ 根据字典内容，添加鱼的个数 """
        if fish_dict.get('gold_fish'):
            self.add_gold_fish(fish_dict.get('gold_fish'))
        if fish_dict.get('octopus_fish'):
            self.add_octopus_fish(fish_dict.get('octopus_fish'))
        if fish_dict.get('manta_fish'):
            self.add_manta_fish(fish_dict.get('manta_fish'))
        if fish_dict.get('shark_fish'):
            self.add_shark_fish(fish_dict.get('shark_fish'))
        if fish_dict.get('boss_fish'):
            self.add_boss_fish(fish_dict.get('boss_fish'))

    def get_field_fishes(self) -> pygame.sprite.Group:
        """ 获取已经在地图场景内，并且存活的鱼 """
        field_fishes = pygame.sprite.Group()
        for fish in self.all_fishes:
            if fish.active and fish.rect.right > 0 and fish.rect.left < WIDTH \
                    and fish.rect.bottom > 0 and fish.rect.top < HEIGHT:
                field_fishes.add(fish)
        return field_fishes

    def remove_all_to_fish_net(self):
        for each in self.fish_nets:
            self.fish_nets.remove(each)

    def remove_all_fish(self):
        for each in self.all_fishes:
            self.fish_nets.remove(each)

    def add_all_fishes_speed(self):
        """ 增加全部鱼的速度 """
        for fish in self.all_fishes:
            fish.speed += 1

    def add_all_fishes_life(self, power: int):
        """ 增加全部鱼的生命 """
        for fish in self.all_fishes:
            fish.max_life = fish.max_life + (fish.max_life * power)


class DifficultyLevel:
    def __init__(self) -> None:
        self.level = 1
        self.difficulty = 0.5
        self.fish_dict = {
            'gold_fish': 0,
            'clown_fish': 0,
            'manta_fish': 0,
            'shark_fish': 0
        }

    def get_level(self) -> int:
        return self.level

    def set_level(self, score, group_control) -> None:
        if self.level == 1 and score > 100000:
            self.level = 2
            print(2)
            self.fish_dict['gold_fish'] = 1
            self.fish_dict['octopus_fish'] = 1
            self.fish_dict['manta_fish'] = 1
            self.fish_dict['shark_fish'] = 1
            group_control.add_fish_to_dict(self.fish_dict)
        elif self.level == 2 and score > 500000:
            print(3)
            self.level = 3
            self.fish_dict['boss_fish'] = 1
            group_control.add_fish_to_dict(self.fish_dict)
        elif self.level == 3 and score > 1000000:
            self.level = 4
            print(4)
            group_control.add_all_fishes_speed()
        elif self.level == 4 and score > 1200000:
            self.level = 5
            print(5)
            group_control.add_all_fishes_life(0.2)
        elif self.level == 5 and score > 1500000:
            self.level = 6
            print(6)
            group_control.add_all_fishes_life(0.2)
        elif self.level == 6 and score > 2000000:
            self.level = 7
            print(7)
            group_control.add_fish_to_dict(self.fish_dict)
            group_control.add_all_fishes_life(0.2)
            group_control.add_all_fishes_speed()


# 游戏的暂停/继续/结束
class GameControl(pygame.sprite.Sprite):
    def __init__(self) -> None:
        super().__init__()
        # 游戏主菜单面板
        self.game_home = pygame.image.load('img/game_home.png').convert_alpha()
        self.game_home_rect = self.game_home.get_rect()
        self.game_home_rect.left = (WIDTH - self.game_home_rect.width) / 2
        self.game_home_rect.top = (HEIGHT - self.game_home_rect.height) / 2
        # 开始游戏图片
        self.game_start = pygame.image.load('img/game_start.png').convert_alpha()
        self.game_start_rect = self.game_start.get_rect()
        self.game_start_rect.left, self.game_start_rect.top = (350, 430)
        # 暂停图片
        self.pause_img = pygame.image.load('img/pause.png').convert_alpha()
        self.pause_rect = self.pause_img.get_rect()
        self.pause_rect.left = (WIDTH - self.pause_rect.width) / 2
        self.pause_rect.top = 10
        # 继续图片
        self.continue_img = pygame.image.load('img/continue.png').convert_alpha()
        self.continue_rect = self.pause_rect
        # 游戏结束图片
        self.game_over_img = pygame.image.load('img/game_over.png').convert_alpha()
        self.game_over_rect = self.game_home_rect
        # 游戏重玩按钮
        self.regame_btn = pygame.image.load('img/regame_btn.png').convert_alpha()
        self.regame_btn_rect = self.game_start_rect
        # 游戏结束按钮
        self.game_over_btn = pygame.image.load('img/game_over_btn.png').convert_alpha()
        self.game_over_btn_rect = self.game_over_btn.get_rect()
        self.game_over_btn_rect.left, self.game_over_btn_rect.top = (650, 430)
        # 当前图片
        self.image = self.continue_img
        self.rect = self.pause_rect
        # 是否为暂停游戏状态
        self.is_pause = False
        # 是否游戏结束
        self.is_game_over = False
        # 是否在主菜单
        self.is_game_home = False

    def game_pause(self):
        self.image = self.pause_img
        self.rect = self.pause_rect
        self.is_pause = True

    def game_continue(self):
        self.image = self.continue_img
        self.rect = self.continue_rect
        self.is_pause = False

    def game_over(self):
        self.image = self.game_over_img
        self.rect = self.game_over_rect
        self.is_game_over = True

    def game_home_menu(self):
        self.image = self.game_home
        self.rect = self.game_home_rect
        self.is_game_home = True

    def write_score(self, score: int):
        if redis_utils:
            score_dict = {'highest': score}
            redis_utils.setDict('score', score_dict)
        else:
            with open('highest_score.txt', 'w') as f:
                f.write(str(score))

    def get_score(self) -> int:
        try:
            if redis_utils:
                score_dict = redis_utils.getDict('score')
                return int(score_dict.get('highest'))
            else:
                with open('highest_score.txt', 'r') as f:
                    return int(f.readline())
        except:
            return 0
